package renguanyu.app.auth4fx.view.role;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import renguanyu.app.auth4fx.App;
import renguanyu.app.auth4fx.controller.role.RoleInfoUpdateController;
import renguanyu.app.auth4fx.dao.role.RolePermissionDao;
import renguanyu.app.auth4fx.model.role.RoleVo;

import java.util.Map;

public class RoleInfoUpdateView extends BorderPane {

    public RoleListView roleListView;
    public RoleInfoUpdateView(RoleListView roleListView){
        this.roleListView = roleListView;
        VBox formPane = createFormPane(); // 表单面板
        HBox bottomBar = createOperatePane(); // 操作面板
        super.setBottom(bottomBar); // 组装
        super.setCenter(formPane);
        initData();
    }

    public StringProperty nameProperty = new SimpleStringProperty();
    public StringProperty permissionProperty = new SimpleStringProperty();
    private VBox createFormPane() {
        Label nameLabel = new Label("角色名：");
//        Label permissionLabel = new Label("权限：");
        TextField nameField = new TextField();
//        TextField permissionField = new TextField();
//        Button permissionUpdateButton = new Button("管理");
//        HBox.setMargin(permissionUpdateButton,new Insets(0,0,0,10));
        HBox nameRow = new HBox(nameLabel, nameField);
//        HBox permissionRow = new HBox(permissionLabel, permissionField);
        VBox formPane = new VBox(nameRow);

        nameLabel.setAlignment(Pos.CENTER_RIGHT);
        nameLabel.setMinWidth(60);

        nameField.textProperty().bindBidirectional(nameProperty);
        HBox.setHgrow(nameField, Priority.ALWAYS);

//        permissionLabel.setAlignment(Pos.CENTER_RIGHT);
//        permissionLabel.setMinWidth(60);

//        permissionField.textProperty().bindBidirectional(permissionProperty);
//        HBox.setHgrow(permissionField,Priority.ALWAYS);

//        permissionUpdateButton.setOnAction(event -> {
//            System.out.println("click permissionUpdateButton");
//            App.mainView.setCenter(new RolePermissionListView(this));
//        });

        nameRow.setAlignment(Pos.CENTER_LEFT);
//        nameRow.setSpacing(10);
        VBox.setMargin(nameRow, new Insets(0, 0, 10, 0));
//        permissionRow.setAlignment(Pos.CENTER_LEFT);
//        permissionRow.setSpacing(10);
//        VBox.setMargin(permissionRow, new Insets(0, 0, 10, 0));
        formPane.setPadding(new Insets(15, 15, 15, 15));
        return formPane;
    }

    private HBox createOperatePane() {
        Button updateBtn = new Button("修改");
        Button backBtn = new Button("返回");
        HBox bottomBar = new HBox(10, updateBtn, backBtn);

        updateBtn.setOnAction(e-> new RoleInfoUpdateController(this).execute());
        backBtn.setOnAction(e-> App.mainView.setCenter(new RoleListView()));
        bottomBar.setAlignment(Pos.CENTER);
        bottomBar.setPadding(new Insets(15, 15, 15, 15));
        return bottomBar;
    }

    public RoleVo selectedRole;
    private void initData() {
        selectedRole = roleListView.selectedRole;
        String id = selectedRole.getId();
        Map<String, Object> role = RolePermissionDao.queryRole(id);
        String roleName = String.valueOf(role.get("role_name"));
        nameProperty.set(roleName);
    }

}
